﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.RootInterface;
using EkStudio.com.bean.xml.RootInterface.Plugin;
using EkStudio.com.common.unit;
using EkStudio.Com.UI.Interface;
using ScintillaNET;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace EkStudio.com.ui.unitbill
{
    public partial class BillJscriptUnitPanel : XtraUserControl,ProjectUnitBillInterface
    {

        private bool readOnly = false;

        private BillExpandJsCodeItem jscript = null;

        private ProjectUnitBillInterface parentPage = null;

        private BillFormatModule module = null;

        private ProjectRootPanel RootPanel = null;

        private bool saveStatus = true;

        public BillJscriptUnitPanel(BillFormatModule _bean, ProjectRootPanel Root, ProjectUnitBillInterface pRoot, BillExpandJsCodeItem code, bool _readOnly)
        {
            InitializeComponent();
            this.module = _bean;
            this.RootPanel = Root;
            this.parentPage = pRoot;
            this.jscript = code;
            readOnly = _readOnly;
            this.InitForm();
        }

        private void InitForm()
        {

            #region 初始编辑器
            this.JScriptMemo.Margins[0].Width = 20;
            this.JScriptMemo.Margins[2].Type = MarginType.Symbol;
            this.JScriptMemo.Margins[2].Mask = Marker.MaskFolders;
            this.JScriptMemo.Margins[2].Sensitive = true;
            this.JScriptMemo.Margins[2].Width = 20;

            JScriptMemo.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            JScriptMemo.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            JScriptMemo.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            JScriptMemo.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            JScriptMemo.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            JScriptMemo.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            JScriptMemo.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            JScriptMemo.StyleResetDefault();
            JScriptMemo.Styles[Style.Default].Font = "Consolas";
            JScriptMemo.Styles[Style.Default].Size = 10;
            JScriptMemo.StyleClearAll();


            // Configure the CPP (C#) lexer styles
            JScriptMemo.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
            JScriptMemo.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JScriptMemo.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
            JScriptMemo.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
            JScriptMemo.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
            JScriptMemo.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
            JScriptMemo.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
            JScriptMemo.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
            JScriptMemo.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
            JScriptMemo.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
            JScriptMemo.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
            JScriptMemo.Lexer = Lexer.Cpp;

            // Set the keywords
            JScriptMemo.SetKeywords(0, "function abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe import virtual while");
            JScriptMemo.SetKeywords(1, "boolean var byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

            //JScriptMemo.SetKeywords(0, common.unit.Label.SQLKeys);
            #endregion


            this.JScriptMemo.Text = jscript.Content;
            this.JScriptMemo.Enabled = !parentPage.ReadOnly;
        }


        public bool isSubUnitBill
        {
            get
            {
                return true;
            }
        }

        public ProjectUnitBillInterface ParentPage
        {
            get
            {
                return parentPage;
            }

            set
            {
                parentPage = value;
            }
        }

        public bool ReadOnly
        {
            get
            {
                return readOnly;
            }

            set
            {
                readOnly = value;
            }
        }

        public string BillKey
        {
            get { return jscript.Key; }
        }

        public bool SaveStatus
        {
            get
            {
                return saveStatus;
            }

            set
            {
                saveStatus = value;
                this.RootPanel.SaveStatus = value;
            }
        }

        public List<ProjectUnitBillInterface> SubjectPage
        {
            get
            {
                return null;
            }
        }

        /// <summary>
        /// 复制
        /// </summary>
        /// <param name="clazz">复制后的clazz</param>
        /// <returns></returns>
        public bool Copy(out object clazz)
        {
            bool val = false;
            clazz = null;

            return val;
        }

        public void AddSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public bool close()
        {
            bool result = true;
            if (!this.saveStatus)
            {
                DialogResult DR = XtraMessageBox.Show("当前数据已更改是否保存?", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (DR == DialogResult.OK)
                {
                    this.saveData(true);
                }
                else if (DR == DialogResult.Cancel)
                {
                    result = false;
                }
            }
            if (result&&this.parentPage!=null)
            {
                this.parentPage.RemoveSubjectBill(this);
                this.parentPage = null;
            }
            return result;
        }

        public bool ContainSubject(ProjectUnitBillInterface bill)
        {
            return false;
        }

        public void RemoveSubjectBill(ProjectUnitBillInterface bill)
        {
            
        }

        public void saveData(bool updateParent)
        {
            this.jscript.Content = this.JScriptMemo.Text;
            
            this.ParentPage.SaveStatus = false;
            if (updateParent)
            {
                this.saveStatus = true;
            }
            else
            {
                this.SaveStatus = true;
            }
        }

        private void JScriptMemo_CharAdded(object sender, CharAddedEventArgs e)
        {
            this.SaveStatus = false;
            if(e.Char == '.')
            {
                int currentPos = JScriptMemo.CurrentPosition;
                string str = JScriptMemo.GetTextRange(currentPos - 2, 1);
                if(str == ")")
                {
                    string allStr = JScriptMemo.GetTextRange(0, currentPos - 2);
                    int number = 0;
                    for(int i = allStr.Length - 1; i >= 0; i--)
                    {
                        char ct = allStr[i];
                        if(ct == '(')
                        {
                            currentPos = currentPos - number -2;
                            break;
                        }
                        number++;
                    }
                }
                int wordStartPos = JScriptMemo.WordStartPosition(currentPos-1, true);
                
                int lenEntered = currentPos - wordStartPos-1;
                string text = JScriptMemo.GetTextRange(wordStartPos, lenEntered);
                if(text == "Record")
                {
                    JScriptMemo.AutoCShow(0,JScriptAutoList.RecordAutoList);
                }
                else if (text.IndexOf("getZdzTab") > -1)
                {
                    JScriptMemo.AutoCShow(0, JScriptAutoList.ZdzTableList);
                }
                else if(text.IndexOf("getField") > -1)
                {
                    JScriptMemo.AutoCShow(0, JScriptAutoList.FieldList);
                }
                else if(text.IndexOf("userInfo") > -1)
                {
                    JScriptMemo.AutoCShow(0, JScriptAutoList.UserInfoList);
                }
                else if(text.IndexOf("getDate") > -1)
                {
                    JScriptMemo.AutoCShow(0, JScriptAutoList.GetDateList);
                }
            }
        }

        private void JScriptMemo_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete || e.KeyCode == Keys.Back)
            {
                SaveStatus = false;
            }
            else if (e.KeyCode == Keys.V && e.Control && Clipboard.ContainsText())
            {
                SaveStatus = false;
            }
        }

        private void JScriptMemo_TextChanged(object sender, EventArgs e)
        {
            //this.SaveStatus = false;
        }
    }
}
